package com.appsindy.common.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.appsindy.common.json.JSONResultCommon;
import com.appsindy.reserv.ReservConstants;
import com.appsindy.reserv.bo.Combo;
import com.appsindy.reserv.service.ReservationServices;
import com.appsindy.reserv.vo.ReserveInfoRoomVO;
import com.appsindy.reserv.vo.ReserveInfoTypeVO;
import com.appsindy.reserv.vo.ReserveInfoVO;
import com.opensymphony.xwork2.Action;


public class ReserveInfoTypeAction extends AppsindyBaseAction {
	
	private static final long serialVersionUID = 1L;
	
	private static final String FWD_BACK 			= "back";
	private static final String FWD_SKIP 			= "skip";
	private static final String FWD_PROCESS_SUCCESS = "processSuccess";
	
	private static final String S_RESERVE_DETAIL 	= "S_RESERVE_DETAIL";
	
	ReserveInfoTypeVO reserveInfoTypeVO;
	
	private List<Combo> typeOfRoomList;
	
	private ReservationServices reservationServices;
	
	private int subGroupId;
	private Date subDateFrom;
	private Date subDateTo;
	private int subTypeOfRoom;
	private String subZone;
	
	private String[] colDateFrom;
	private String[] colDateTo;
	private String[] colTypeOfRoomId;
	private String[] colTypeOfRoom;
	private String[] colZone;
	private String[] colFloor;
	private String[] colDay;
	private String[] colQtyRoomPerDay;
	private String[] colRatePerRoom;
	private String[] colTotalAmount;
	private String[] colBreakfast;
	
	public String execute() {
		log.info("Init Reservation Information Type of Room ...");

		reserveInfoTypeVO = (ReserveInfoTypeVO)getSession().get(ReservConstants.S_RESERVE);
		
		
		getSession().remove(ReservConstants.S_RESERVE);
		return initialReserve();
	}
	
	private String initialReserve(){
		log.info("Initial Reservation Information ...");
		setTypeOfRoomList(getReservationServices().getTypeOfRoomCombos());
		
		setReserveInfoTypeVO(getReservationServices().selectAvailableTypeOfRoom(getReserveInfoTypeVO()));
		
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")	
	public String search() {
		log.info("Search Reservation Information ...");
		
		try{
			manageDetailList();
		}catch(Exception e){
			
		}
		
		getSession().put(S_RESERVE_DETAIL, reserveInfoTypeVO.getReserveList());

		return initialReserve();
	}
	
	public String reserve() {
		log.info("Reserve ...");
		
		return processReserve(ReservConstants.ServiceStatusReserved);
	}
	
	public String book() {
		log.info("Book ...");

		return processReserve(ReservConstants.ServiceStatusBooked);
	}

	public String checkin() {
		log.info("Checkin ...");
		
		return processReserve(ReservConstants.ServiceStatusCheckin);
	}
	
	private void manageDetailList() throws Exception{
		log.info("manageDetailList ...");
        List<ReserveInfoVO> resultList = new ArrayList<ReserveInfoVO>();
        if(colDateFrom != null && colDateFrom.length > 0){
            int size			= colDateFrom.length;

            ReserveInfoVO vo	= null;
            for(int i=0; i<size; i++){
                vo              = new ReserveInfoVO();
                vo.setDateFrom(colDateFrom[i]);
                vo.setDateTo(colDateTo[i]);
            	vo.setTypeOfRoomId(colTypeOfRoomId[i]);
            	vo.setTypeOfRoom(colTypeOfRoom[i]);
            	vo.setZone(colZone[i]);
            	vo.setFloor(colFloor[i]);
            	vo.setDay(colDay[i]);
            	vo.setQtyRoomPerDay(colQtyRoomPerDay[i]);
            	vo.setRatePerRoom(colRatePerRoom[i]);
            	vo.setTotalAmount(colTotalAmount[i]);
            	vo.setBreakfast(colBreakfast[i]==null||"".equals(colBreakfast[i])?"0":"1");	                
                
                resultList.add(vo);
            }
        }
        getReserveInfoTypeVO().setReserveList(resultList);
	}
	
	@SuppressWarnings("unchecked")
	private String processReserve(String serviceStatus){
		log.info("Process Reserve ...");
		
		try{
			manageDetailList();
		}catch(Exception e){
			
		}
		
		reserveInfoTypeVO = getReservationServices().processReserve(getReserveInfoTypeVO(), serviceStatus);
		
		if(reserveInfoTypeVO.getServiceHeaderId() > 0){
			
			//set for room
			ReserveInfoRoomVO infoVO = new ReserveInfoRoomVO();
			infoVO.setDateFrom(reserveInfoTypeVO.getDateFrom());
			infoVO.setDateTo(reserveInfoTypeVO.getDateTo());
			infoVO.setGroupInfoVO(reserveInfoTypeVO.getGroupInfoVO());
			infoVO.setServiceHeaderId(reserveInfoTypeVO.getServiceHeaderId());
			getSession().put(ReservConstants.S_RESERVE, infoVO);
			
			return FWD_PROCESS_SUCCESS;
		}else{
			getSession().put(S_RESERVE_DETAIL, reserveInfoTypeVO.getReserveList());
			
			return initialReserve();
		}
	}

	@SuppressWarnings("finally")
	public String getZoneListJSON(){
    	log.info("getZoneListJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't get zone list");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	List<Combo> result = getReservationServices().getZoneCombos(subTypeOfRoom);
            jsonResultCommon = new JSONResultCommon(Action.SUCCESS, result);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }
    
    @SuppressWarnings("finally")
	public String getFloorListJSON(){
    	log.info("getFloorListJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't get floor list");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	List<Combo> result = getReservationServices().getFloorCombos(subTypeOfRoom, subZone);
            jsonResultCommon = new JSONResultCommon(Action.SUCCESS, result);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }    
    
    @SuppressWarnings("finally")
	public String getRoomRateJSON(){
    	log.info("getRoomRateJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't get room rate");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	ReserveInfoVO infoVO = getReservationServices().getRoomRate(subGroupId, subDateFrom, subDateTo, subTypeOfRoom);
        	
        	if(infoVO.getErrCode() < 0){
        		infoVO.setErrMsg(getText("reserve.typeofroom.error.manyroomrate"));
        	}
            jsonResultCommon = new JSONResultCommon(Action.SUCCESS, infoVO);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }    
    
    @SuppressWarnings({ "finally", "unchecked" })
	public String generateReserveInfoJSON(){
    	log.info("generateReserveInfoJSON ...");
        Map<String, Object> errorMap = new HashMap<String, Object>();
        errorMap.put("errorStr", "Can't get reserve information detail");
        JSONResultCommon jsonResultCommon = new JSONResultCommon(Action.ERROR, errorMap);
        try{
        	ArrayList<ReserveInfoVO> detail = (ArrayList)getSession().get(S_RESERVE_DETAIL);
        	getSession().remove(S_RESERVE_DETAIL);        	
        	
            jsonResultCommon = new JSONResultCommon(Action.SUCCESS, detail);
        }catch (Exception ex) {
            System.out.println("=========================== Exception in class : " + this);
            ex.printStackTrace();
        } finally {
            setJsonModel(jsonResultCommon);
            return JSON_RESULT;
        }
    }       
    
	public String back() {
		log.info("Back ...");
		
		if(reserveInfoTypeVO.getSeqId() > 0){
			getReservationServices().clearTemp(reserveInfoTypeVO.getSeqId());
		}		
		return FWD_BACK;
	}
	
	@SuppressWarnings("unchecked")
	public String skip() {
		log.info("Skip ...");
		
		if(reserveInfoTypeVO.getSeqId() > 0){
			getReservationServices().clearTemp(reserveInfoTypeVO.getSeqId());
		}		
		
		//set for room
		ReserveInfoRoomVO infoVO = new ReserveInfoRoomVO();
		infoVO.setServiceHeaderId(reserveInfoTypeVO.getServiceHeaderId());
		infoVO.setDateFrom(reserveInfoTypeVO.getDateFrom());
		infoVO.setDateTo(reserveInfoTypeVO.getDateTo());
		infoVO.setGroupInfoVO(reserveInfoTypeVO.getGroupInfoVO());
		getSession().put(ReservConstants.S_RESERVE, infoVO);
		
		return FWD_SKIP;
	}	
	
	public ReserveInfoTypeVO getReserveInfoTypeVO() {
		return reserveInfoTypeVO;
	}

	public void setReserveInfoTypeVO(ReserveInfoTypeVO reserveInfoTypeVO) {
		this.reserveInfoTypeVO = reserveInfoTypeVO;
	}

	public List<Combo> getTypeOfRoomList() {
		return typeOfRoomList;
	}

	public void setTypeOfRoomList(List<Combo> typeOfRoomList) {
		this.typeOfRoomList = typeOfRoomList;
	}

	public ReservationServices getReservationServices() {
		return reservationServices;
	}

	public void setReservationServices(ReservationServices reservationServices) {
		this.reservationServices = reservationServices;
	}

	public int getSubTypeOfRoom() {
		return subTypeOfRoom;
	}

	public void setSubTypeOfRoom(int subTypeOfRoom) {
		this.subTypeOfRoom = subTypeOfRoom;
	}

	public String getSubZone() {
		return subZone;
	}

	public void setSubZone(String subZone) {
		this.subZone = subZone;
	}

	public String[] getColDateFrom() {
		return colDateFrom;
	}

	public void setColDateFrom(String[] colDateFrom) {
		this.colDateFrom = colDateFrom;
	}

	public String[] getColDateTo() {
		return colDateTo;
	}

	public void setColDateTo(String[] colDateTo) {
		this.colDateTo = colDateTo;
	}

	public String[] getColTypeOfRoomId() {
		return colTypeOfRoomId;
	}

	public void setColTypeOfRoomId(String[] colTypeOfRoomId) {
		this.colTypeOfRoomId = colTypeOfRoomId;
	}

	public String[] getColTypeOfRoom() {
		return colTypeOfRoom;
	}

	public void setColTypeOfRoom(String[] colTypeOfRoom) {
		this.colTypeOfRoom = colTypeOfRoom;
	}

	public String[] getColZone() {
		return colZone;
	}

	public void setColZone(String[] colZone) {
		this.colZone = colZone;
	}

	public String[] getColFloor() {
		return colFloor;
	}

	public void setColFloor(String[] colFloor) {
		this.colFloor = colFloor;
	}

	public String[] getColDay() {
		return colDay;
	}

	public void setColDay(String[] colDay) {
		this.colDay = colDay;
	}

	public String[] getColQtyRoomPerDay() {
		return colQtyRoomPerDay;
	}

	public void setColQtyRoomPerDay(String[] colQtyRoomPerDay) {
		this.colQtyRoomPerDay = colQtyRoomPerDay;
	}

	public String[] getColRatePerRoom() {
		return colRatePerRoom;
	}

	public void setColRatePerRoom(String[] colRatePerRoom) {
		this.colRatePerRoom = colRatePerRoom;
	}

	public String[] getColTotalAmount() {
		return colTotalAmount;
	}

	public void setColTotalAmount(String[] colTotalAmount) {
		this.colTotalAmount = colTotalAmount;
	}

	public String[] getColBreakfast() {
		return colBreakfast;
	}

	public void setColBreakfast(String[] colBreakfast) {
		this.colBreakfast = colBreakfast;
	}

	public int getSubGroupId() {
		return subGroupId;
	}

	public void setSubGroupId(int subGroupId) {
		this.subGroupId = subGroupId;
	}

	public Date getSubDateFrom() {
		return subDateFrom;
	}

	public void setSubDateFrom(Date subDateFrom) {
		this.subDateFrom = subDateFrom;
	}

	public Date getSubDateTo() {
		return subDateTo;
	}

	public void setSubDateTo(Date subDateTo) {
		this.subDateTo = subDateTo;
	}
    
}
